package com.quick.develop.flowable.controller.task;

import com.quick.develop.flowable.domain.dto.task.BpmTaskDTO;
import com.quick.develop.flowable.domain.vo.task.BpmAddMultiExtReqVO;
import com.quick.develop.flowable.domain.vo.task.BpmTaskRejectReqVO;
import com.quick.develop.flowable.domain.vo.task.BpmTaskReqVO;
import com.quick.develop.flowable.domain.vo.task.BpmTaskUpdateAssigneeReqVO;
import com.quick.develop.framework.common.page.R;
import com.quick.develop.flowable.application.IBpmTaskApplication;
import com.quick.develop.flowable.domain.task.BpmTaskDonePageReqVO;
import com.quick.develop.flowable.domain.task.BpmTaskTodoExternalReqVO;
import com.quick.develop.flowable.domain.task.BpmTaskTodoPageReqVO;
import com.quick.develop.flowable.service.task.IBpmTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author junyuan.liu
 * @description:
 * @date 2022/4/26 10:21
 */
@Api(tags = "流程管理后台 - 流程任务实例")
@RestController
@RequestMapping("/task")
@Validated
public class BpmTaskController {
    @Resource
    private IBpmTaskService bpmTaskService;

    @Resource
    private IBpmTaskApplication bpmTaskApplication;

    @GetMapping("/todo-page")
    @ApiOperation("获取 Todo 待办任务分页")
    public R<?> getTodoTaskPage(@Valid BpmTaskTodoPageReqVO pageVO) {
        return R.ok(bpmTaskService.getTodoTaskPage(pageVO));
    }

    @GetMapping("/todo-count")
    @ApiOperation("获取待办数量")
    public R<?> getTodoCount() {
        return R.ok(bpmTaskService.getTodoCount());
    }

    @GetMapping("/done-page")
    @ApiOperation("获取已办任务分页")
    public R<?> getDoneTaskPage(@Valid BpmTaskDonePageReqVO pageVO) {
        return R.ok(bpmTaskService.getDoneTaskPage(pageVO));
    }

    @GetMapping("/done-page-2")
    @ApiOperation("获取已办任务分页-2")
    public R<?> getDoneTaskPage2(@Valid BpmTaskDonePageReqVO pageVO) {
        return R.ok(bpmTaskService.getDoneTaskPage2(pageVO));
    }

    @GetMapping("/done-count")
    @ApiOperation("获取已办数量")
    public R<?> getDoneCount() {
        return R.ok(bpmTaskService.getDoneCount());
    }


    @PostMapping(value = "/complete-task")
    @ApiOperation(value = "审批任务")
    public R<?> complete(@Valid @RequestBody BpmTaskReqVO taskVo) {
        bpmTaskApplication.complete(taskVo);
        return R.ok();
    }

    @PostMapping(value = "/rollback-task-actIds")
    @ApiOperation(value = "根据节点key退回任务", notes = "根据多个节点key进行退回 可以退回多个节点到目的节点")
    public R<?> rollbackTaskByActivityIds(@Valid @RequestBody BpmTaskRejectReqVO rejVO) {
        bpmTaskApplication.rollbackTaskByActivityIds(rejVO);
        return R.ok();
    }

    @PostMapping(value = "/rollback-task-taskId")
    @ApiOperation(value = "根据任务ID退回任务", notes = "根据taskID 进行退回 适用于串行的流程")
    public R<?> rollbackTaskByTaskId(@Valid @RequestBody BpmTaskRejectReqVO rejVO) {
        bpmTaskApplication.rollbackTaskByTaskId(rejVO);
        return R.ok();
    }

    @GetMapping(value = "/findReturnTaskList")
    @ApiOperation(value = "获取所有可回退的节点")
    public R<?> findReturnTaskList(BpmTaskReqVO taskVo) {
        return R.ok(bpmTaskService.findReturnTaskList(taskVo));
    }

    @PostMapping(value = "/claim-task")
    @ApiOperation(value = "认领/签收任务")
    public R<?> claim(@Valid @RequestBody BpmTaskReqVO taskVo) {
        bpmTaskApplication.claim(taskVo);
        return R.ok();
    }

    @PostMapping(value = "/unClaim-task")
    @ApiOperation(value = "取消认领/签收任务")
    public R<?> unClaim(@Valid @RequestBody BpmTaskReqVO taskVo) {
        bpmTaskApplication.unClaim(taskVo);
        return R.ok();
    }

    @PostMapping(value = "/delegate-task")
    @ApiOperation(value = "委派任务")
    public R<?> delegate(@Valid @RequestBody BpmTaskUpdateAssigneeReqVO taskVo) {
        bpmTaskApplication.delegate(taskVo);
        return R.ok();
    }

    @PostMapping(value = "/assign-task")
    @ApiOperation(value = "转办任务")
    public R<?> assignTask(@Valid @RequestBody BpmTaskUpdateAssigneeReqVO assigneeReqVO) {
        bpmTaskApplication.assignTask(assigneeReqVO);
        return R.ok();
    }

    @PostMapping(value = "/stop-task")
    @ApiOperation(value = "终止流程")
    public R<?> stopTask(@Valid @RequestBody BpmTaskReqVO taskVo) {
        bpmTaskApplication.stopTask(taskVo);
        return R.ok();
    }

    @PostMapping(value = "/super-assign")
    @ApiOperation(value = "更改任务人", notes = "更改任务处理人，慎用")
    public R<?> assign(@Valid @RequestBody BpmTaskUpdateAssigneeReqVO assigneeReqVO) {
        bpmTaskService.assign(assigneeReqVO.getTaskId(), assigneeReqVO.getAssignee());
        return R.ok();
    }

    @GetMapping("/list-by-process-instance-id")
    @ApiOperation(value = "获得指定流程实例的任务列表", notes = "包括完成的、未完成的")
    @ApiImplicitParam(name = "processInstanceId", value = "流程实例的编号", required = true, dataTypeClass = String.class)
    public R<?> getTaskListByProcessInstanceId(@RequestParam("processInstanceId") String processInstanceId) {
        return R.ok(bpmTaskService.getTaskListByProcessInstanceId(processInstanceId));
    }

    @GetMapping("/getTaskListByBusinessKey")
    @ApiOperation(value = "通过实例businessKey获取到任务")
    @ApiImplicitParam(name = "businessKey", value = "业务KEY", required = true, dataTypeClass = String.class)
    public R<?> getTaskListByBusinessKey(@RequestParam("businessKey") String businessKey) {
        return R.ok(bpmTaskService.getTaskListByBusinessKey(businessKey));
    }

    @GetMapping("/getRuTaskListByBusinessKey/{businessKey}")
    @ApiOperation(value = "通过实例businessKey获取到任务")
    @ApiImplicitParam(name = "businessKey", value = "业务KEY", required = true, dataTypeClass = String.class)
    public R<List<BpmTaskDTO>> getRuTaskListByBusinessKey(@PathVariable("businessKey") String businessKey) {
        return R.ok(bpmTaskService.getRuTaskListByBusinessKey(businessKey));
    }

    @PostMapping(value = "/addMultiInstanceExecution")
    @ApiOperation(value = "多实例动态加人")
    public R<?> addMultiInstanceExecution(@Valid @RequestBody BpmAddMultiExtReqVO reqVo) {
        bpmTaskApplication.addMultiInstanceExecution(reqVo);
        return R.ok();
    }

    @PostMapping(value = "/addCandidateUser")
    @ApiOperation(value = "动态增加候选人")
    public R<?> addCandidateUser(@Valid @RequestBody BpmTaskReqVO reqVo) {
        bpmTaskApplication.addCandidateUser(reqVo);
        return R.ok();
    }

    @PostMapping(value = "/addCandidateGroup")
    @ApiOperation(value = "动态增加候选组")
    public R<?> addCandidateGroup(@Valid @RequestBody BpmTaskReqVO reqVo) {
        bpmTaskApplication.addCandidateGroup(reqVo);
        return R.ok();
    }

    @PostMapping("/get-next-tasks")
    @ApiOperation(value = "获取下一个节点集合")
    public R<?> getNextTaskList(@RequestBody BpmTaskReqVO reqVo) {
        return R.ok(bpmTaskService.getNextTaskList(reqVo));
    }

    @PostMapping("/withdraw-task")
    @ApiOperation(value = "撤回")
    public R<?> withdrawTask(@RequestBody BpmTaskReqVO reqVo) {
        bpmTaskApplication.withdrawTask(reqVo);
        return R.ok();
    }

    @GetMapping("/getMainProcessHisTasksByBusinessKey/{businessKey}")
    @ApiOperation(value = "通过实例businessKey获取到主流程已完成任务")
    public R<List<BpmTaskDTO>> getMainProcessHisTasksByBusinessKey(@PathVariable("businessKey") String businessKey) {
        return R.ok(bpmTaskApplication.getMainProcessHisTasksByBusinessKey(businessKey));
    }

    @GetMapping("/is-run-task-multi/{taskId}")
    @ApiOperation(value = "运行的任务是否是会签点")
    public R<Boolean> isRunTaskMulti(@PathVariable("taskId") String taskId) {
        return R.ok(bpmTaskService.isTaskMulti(taskId));
    }

    @GetMapping("/getRunMultiTaskInfo/{taskId}")
    @ApiOperation(value = "获取会签任务详情")
    public R<?> getRunMultiTaskInfo(@PathVariable("taskId") String taskId) {
        return R.ok(bpmTaskApplication.getRunMultiTaskInfo(taskId));
    }

    @GetMapping("/get-recently-task/{processInstanceId}")
    @ApiOperation(value = "获取会签任务详情")
    public R<?> getRecentlyTask(@PathVariable("processInstanceId") String id) {
        return R.ok(bpmTaskApplication.getRecentlyTask(id));
    }

    @GetMapping("/get-entrust-owner/{taskId}")
    @ApiOperation(value = "获取原始委托人")
    public R<?> getEntrustOwner(@PathVariable("taskId") String id) {
        return R.ok(bpmTaskApplication.getEntrustOwner(id));
    }
}
